package com.ssm.aop;
import com.ssm.annotation.DataSource;
import com.ssm.multiple.MultipleDataSource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
 * @author zchcpywin10
 */
@Component
@Slf4j
@Aspect
@Order(-1)
public class DataSourceAspect {
  @Pointcut("@within(com.ssm.annotation.DataSource) || @annotation(com.ssm.annotation.DataSource)")
  public void pointCut() {
    // TO DO document why this method is empty.
  }
  @Before("pointCut() && @annotation(dataSource)")
  public void doBefore(DataSource dataSource) {
    String ds = dataSource.value().getValue();
    log.info("ds：{}", ds);
    MultipleDataSource.setDataSource(ds);
  }
  @After("pointCut()")
  public void doAfter() {
    MultipleDataSource.clear();
  }
}
